//
//  JYShowView.m
//  JYFarm
//
//  Created by 花花 on 2017/10/13.
//  Copyright © 2017年 花花. All rights reserved.
//

#import "JYShowView.h"

@interface JYShowView()
//!
@property(nonatomic)UIImageView *imgView;
//
@property(nonatomic)UILabel *showLabel;
//取消btn
@property(nonatomic)UIButton *cancelBtn;
//确定btn
@property(nonatomic)UIButton *doneBtn;

@property(nonatomic)NSString *text;

@property(nonatomic,assign)BOOL isSected;

@property(nonatomic,copy)ShowBtnBolck btnBolck;


@end

@implementation JYShowView

- (instancetype)showText:(NSString *)text isSected:(BOOL)isSected btnHandler:(ShowBtnBolck)hander{


    if (self == [super init]) {
        _text = text;
        _isSected = isSected;
        _btnBolck = hander;
        
        [self setupSubView];
        [self initConstranints];
    }
    return self;

}

- (UIImageView *)imgView{

    if (!_imgView) {
        _imgView = [[UIImageView alloc]init];
        _imgView.image = [UIImage imageNamed:@"popover_background"];
    }
    return _imgView;

}
- (UILabel *)showLabel{

    if (!_showLabel) {
        _showLabel = [UILabel labelText:@"" fontSize:12 textColor:k6Color textAlignment:NSTextAlignmentCenter];
        
    }
    return _showLabel;
}

- (void)addBtns{
    
    _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _cancelBtn.tag = JYShowTypeCancel;
    [_cancelBtn addTarget:self action:@selector(btnOnCclick:) forControlEvents:UIControlEventTouchUpInside];
    
    [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    if (_isSected == YES) {
    
        
        [_cancelBtn setTitleColor:kfColor forState:UIControlStateNormal];
        [_cancelBtn setBackgroundImage:[UIImage imageNamed:@"popover_frame2"] forState:UIControlStateNormal];
    }else{
    
        [_cancelBtn setTitleColor:k9Color forState:UIControlStateNormal];
        [_cancelBtn setBackgroundImage:[UIImage imageNamed:@"popover_frame"] forState:UIControlStateNormal];
    }

    _cancelBtn.titleLabel.font = kFont(13);
    
    [self addSubview:_cancelBtn];
    
    _doneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _doneBtn.tag = JYShowTypeDone;
    [_doneBtn addTarget:self action:@selector(btnOnCclick:) forControlEvents:UIControlEventTouchUpInside];
    [_doneBtn setTitle:@"确定" forState:UIControlStateNormal];
    if (_isSected == YES) {
        
        [_doneBtn setTitleColor:k9Color forState:UIControlStateNormal];
        [_doneBtn setBackgroundImage:[UIImage imageNamed:@"popover_frame"] forState:UIControlStateNormal];
       
    }else{
        [_doneBtn setTitleColor:kfColor forState:UIControlStateNormal];
        [_doneBtn setBackgroundImage:[UIImage imageNamed:@"popover_frame2"] forState:UIControlStateNormal];
     
    }
    
    _doneBtn.titleLabel.font = kFont(13);
    
    [self addSubview:_doneBtn];
    
}

- (void)btnOnCclick:(UIButton *)btn{

    if (_btnBolck) {
        _btnBolck(btn.tag);
    }

}
- (void)setupSubView{

    
    self.backgroundColor  =[UIColor whiteColor];
    self.layer.cornerRadius = 20;

    
    
    [self addSubview:self.imgView];

    [self addSubview:self.showLabel];
    
    _showLabel.text = _text;
    
    [self  addBtns];

    
}

- (void)initConstranints{
    JYWeakSelf;
    [_imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(weakSelf.mas_top);
        make.left.mas_equalTo(weakSelf.mas_left);
        make.height.mas_equalTo(100);
        make.width.mas_equalTo(weakSelf.mas_width);
    }];
    
    [_showLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.imgView.mas_bottom).offset(20);
        make.centerX.equalTo(weakSelf.mas_centerX);


        
    }];
    
   
    [_cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.mas_left).offset(20);
        make.bottom.mas_equalTo(weakSelf.mas_bottom).offset(-20);
        make.right.mas_equalTo(weakSelf.doneBtn.mas_left).offset(-20);
        make.width.mas_equalTo(weakSelf.doneBtn.mas_width);
        
        
    }];
    
    [_doneBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(weakSelf.mas_right).offset(-20);
        make.top.mas_equalTo(weakSelf.cancelBtn.mas_top);
        make.bottom.mas_equalTo(weakSelf.cancelBtn.mas_bottom);
        
        
    }];
    [_cancelBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(40);
    }];

}

@end


@interface JYNoGoodsShowView()
//
@property(nonatomic)UIImageView *imgView;
//
@property(nonatomic)UILabel *showLabel;
//确定btn
@property(nonatomic)UIButton *confirmBtn;

@property(nonatomic)NSString *showText;


@property(nonatomic)UIView *bgView;

@property(nonatomic,copy)ButtonOnClickHandler onClickHandler;


@end

@implementation JYNoGoodsShowView

- (instancetype)showText:(NSString *)text  onClickHandler:(ButtonOnClickHandler)onClickHandler{
    
    if (self == [super init]) {
  
        _showText = text;
        
        _onClickHandler = [onClickHandler copy];
        self.showLabel.text = text;
        [self setupSubView];
        [self initConstranints];
    }
    return self;


}
- (UIImageView *)imgView{
    
    if (!_imgView) {
        _imgView = [[UIImageView alloc]init];
        _imgView.image = [UIImage imageNamed:@"popover_background"];
    }
    return _imgView;
    
}
- (UILabel *)showLabel{
    
    if (!_showLabel) {
        _showLabel = [UILabel labelText:_showText fontSize:12 textColor:k6Color textAlignment:NSTextAlignmentCenter];
        
    }
    return _showLabel;
}

- (UIButton *)confirmBtn{
    if (!_confirmBtn) {
        
        _confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [_confirmBtn addTarget:self action:@selector(confirmBtnOnClick) forControlEvents:UIControlEventTouchUpInside];
        [_confirmBtn setTitle:@"确定" forState:UIControlStateNormal];
        
        [_confirmBtn setTitleColor:kfColor forState:UIControlStateNormal];
        [_confirmBtn setBackgroundImage:[UIImage imageNamed:@"popover_frame3"] forState:UIControlStateNormal];

    
        _confirmBtn.titleLabel.font = kFont(13);
        

    }
    
    return _confirmBtn;
    
}

- (void)setupSubView{
    
    self.backgroundColor  =[UIColor whiteColor];
    self.layer.cornerRadius = 20;
    
    [self addSubview:self.imgView];
    
    [self addSubview:self.showLabel];

    [self  addSubview:self.confirmBtn];
    
    
}

- (void)initConstranints{
    JYWeakSelf;
    [_imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(weakSelf.mas_top);
        make.left.mas_equalTo(weakSelf.mas_left);
        make.height.mas_equalTo(100);
        make.width.mas_equalTo(weakSelf.mas_width);
    }];
    
    [_showLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.imgView.mas_bottom).offset(20);
        make.centerX.equalTo(weakSelf.mas_centerX);
        
        
        
    }];
    
    [_confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.mas_left).offset(10);
        make.bottom.mas_equalTo(weakSelf.mas_bottom).offset(-10);
        make.right.mas_equalTo(weakSelf.mas_right).offset(-10);
        make.height.mas_equalTo(40);
        
        
    }];

}

// 按钮点击响应事件
- (void)confirmBtnOnClick{
    if (_onClickHandler) {
        
        _onClickHandler();
    }
}
@end

